package servlet;

import service.TransferService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "TransferServlet")
public class TransferServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
//        接收转账的参数
        String out1 = request.getParameter("out");
        int out = Integer.parseInt(out1);
        String in1 = request.getParameter("in");
        int in = Integer.parseInt(in1);
        String strMoney = request.getParameter("money");
        double money = Double.parseDouble(strMoney);
        if ((in1 == null || in1 == "") || (strMoney == null || strMoney == "")) {
            System.out.println("转入账户或转入金额格式不正确");
        } else {
//        调用业务层处理表单数据
            TransferService service = new TransferService();
            int isTransferSuccess = service.transfer(out, in, money);

            //下面出现中文，进行转码
            response.setContentType("text/html;charset=UTF-8");
            if (isTransferSuccess == 1) {
                response.getWriter().write("转账成功！！！");
            } else if (isTransferSuccess == 2) {
                response.getWriter().write("转账失败！！！");
            } else {
                response.getWriter().write("您的余额不足!!!");
            }
        }
    }
}
